import { createRouter, createWebHistory } from 'vue-router'

// 删除TypeScript类型声明和重复的routes声明
const routes = [
  {
    path: '/',
    name: 'home',
    component: () => import('@/views/HomePage.vue'),
    meta: { title: 'routeTitles.home' }
  },
  {
    path: '/product-categories',
    name: 'product-categories',
    components: {
      default: () => import('@/views/ProductCategories.vue')
    },
    meta: { title: 'routeTitles.product-categories' },
    children: [
      {
        path: '',
        name: 'default-redirect',
        redirect: { name: 'products' }
      },
      {
        path: ':category?',  // 添加可选参数标识
        name: 'products',
        component: () => import('@/views/ProductsView.vue'),
        props: true,
        children: [
          {
            path: 'soft-pouches',
            component: () => import('@/views/products/SoftPouches.vue'),
            meta: { 
              title: 'routeTitles.softPouches',
              productType: 'flexible-packaging',
              breadcrumb: ['product-categories', 'soft-pouches']
            }
          },
          {
            path: 'aluminum-foil-bags',
            component: () => import('@/views/products/AluminumFoamBags.vue'),
            meta: {
              title: 'routeTitles.aluminumFoilBags', 
              productType: 'high-barrier',
              breadcrumb: ['product-categories', 'aluminum-foil-bags']
            }
          },
          {
            path: 'vacuum-packaging',
            component: () => import('@/views/products/VacuumPackaging.vue'),
            meta: {
              title: 'routeTitles.vacuumPackaging',
              productType: 'vacuum-sealed',
              breadcrumb: ['product-categories', 'vacuum-packaging']
            }
          },
          {
            path: 'retort-pouches',
            component: () => import('@/views/products/RetortPouches.vue'),
            meta: {
              title: 'routeTitles.retortPouches',
              productType: 'high-temperature',
              breadcrumb: ['product-categories', 'retort-pouches']
            }
          },
          {
            path: 'stand-up-zip-bags',
            component: () => import('@/views/products/StandUpZipBags.vue'),
            meta: {
              title: 'routeTitles.standUpZipBags',
              productType: 'resealable',
              breadcrumb: ['product-categories', 'stand-up-zip-bags']
            }
          }
        ],
        meta: { 
          title: 'routeTitles.products',
          requiresAuth: false,
          breadcrumb: ['product-categories', 'products']
        }
      }
    ]
  },
  {
    path: '/production-capability',
    name: 'production-capability',
    component: () => import('@/views/ProductionCapability.vue'),
    meta: { title: 'routeTitles.productionCapability' }
  },
  {
    path: '/client-cases',
    name: 'client-cases',
    component: () => import('@/views/ClientCases.vue'),
    meta: { title: 'routeTitles.client-cases' }
  },
  {
    path: '/factory-certifications',
    name: 'factory-certifications',
    component: () => import('@/views/FactoryCertifications.vue'),
    meta: { title: 'routeTitles.factory-certifications' }
  },
  {
    path: '/contact-us',
    name: 'contact-us',
    component: () => import('@/views/ContactUs.vue'),
    meta: { title: 'routeTitles.contact-us' }
  },
  {
    path: '/package-designer',
    name: 'package-designer',
    component: () => import('@/views/PackageDesigner.vue'),
    meta: { 
      title: 'routeTitles.packageDesigner',
      breadcrumb: ['home', 'package-designer']
    }
  },
  {
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/NotFound.vue'),
    meta: { title: 'routeTitles.notFound' }
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 全局路由守卫
router.beforeEach((to) => {
  document.title = to.meta.title 
    ? `${to.meta.title} - ${import.meta.env.VITE_APP_TITLE}` 
    : import.meta.env.VITE_APP_TITLE
})

// 路由错误处理
router.onError((error) => {
  console.error('路由错误：', error)
})

export default router
